﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Reflection;



namespace GISforTT_API
{
    public class CornerMenusMgr
    {
        public enum CornerMenuType
        {
            FourCornerMenus,
            TwoCornerMenus,
            NoCornerMenus
        };

        public enum CornerMenuFunction
        {
            ExitFunction,
            //SearchFunction,
            BookmarkListFunction,
            LayersMgrFunction,
            CustomFunction
        };


        #region members

        CornerMenuType menuType = CornerMenuType.NoCornerMenus;
        List<CornerMenuFunction> menuFunctions = null;

        public CornerMenu FunctionMenuUpperLeft;
        public CornerMenu FunctionMenuBottomLeft;
        public CornerMenu FunctionMenuUpperRight;
        public CornerMenu FunctionMenuBottomRight;

        private ExitDialog ExitDialogUpperLeft;
        private ExitDialog ExitDialogUpperRight;
        private ExitDialog ExitDialogBottomLeft;
        private ExitDialog ExitDialogBottomRight;

        Canvas InterfaceLayoutRoot;

        #endregion

        #region properties

        /// <summary>
        /// Menu Type
        /// </summary>
        public CornerMenuType MenuType
        {
            get
            {
                return menuType;
            }

            set
            {
                menuType = value;
                // TODO: update the menus of the app
            }
        }

        /// <summary>
        /// Menu Function Types
        /// </summary>
        public List<CornerMenuFunction> MenuFunctions
        {
            get
            {
                if (menuFunctions == null)
                {
                    initializeDefaultMenuFunctions();
                }
                return menuFunctions;
            }

            set
            {
                menuFunctions = value;

                // TODO: update the app
            }
        }

        #endregion

        /// <summary>
        /// Returns the instance of the CornerMenuMgr singleton class
        /// </summary>
        public static CornerMenusMgr Instance { get; private set; }

        static CornerMenusMgr() { Instance = new CornerMenusMgr(); }

        CornerMenusMgr() {}

        /// <summary>
        /// Sets the layout root canvas of the application
        /// </summary>
        /// <param name="LayoutRoot">Main Canvas</param>
        public void setLayoutRoot(Canvas LayoutRoot)
        {
            InterfaceLayoutRoot = LayoutRoot;
        }

        /// <summary>
        /// Initializes the default list of menu functions
        /// </summary>
        public void initializeDefaultMenuFunctions()
        {
            menuFunctions = new List<CornerMenuFunction>();
            menuFunctions.Add(CornerMenuFunction.LayersMgrFunction);
            //menuFunctions.Add(CornerMenuFunction.SearchFunction);
            menuFunctions.Add(CornerMenuFunction.BookmarkListFunction);
            menuFunctions.Add(CornerMenuFunction.ExitFunction);            
        }

        /// <summary>
        /// Creates the Corner menus according to the properties set in the manager
        /// </summary>
        public void CreateCornerMenus()
        {
            if (MenuType == CornerMenuType.NoCornerMenus) return;

            var offset = -150;

            // Bottom Left Corner menu
            FunctionMenuBottomLeft = new CornerMenu(CornerMenu.CornerMenuLocation.BottomLeft);
            InterfaceLayoutRoot.Children.Add(FunctionMenuBottomLeft);
            Canvas.SetLeft(FunctionMenuBottomLeft, offset);
            Canvas.SetBottom(FunctionMenuBottomLeft, offset);
            Canvas.SetZIndex(FunctionMenuBottomLeft, 10);

            FunctionMenuBottomLeft.LayoutTransform = new RotateTransform(90);

            // Upper Right Corner menu
            FunctionMenuUpperRight = new CornerMenu(CornerMenu.CornerMenuLocation.TopRight);
            InterfaceLayoutRoot.Children.Add(FunctionMenuUpperRight);
            Canvas.SetTop(FunctionMenuUpperRight, offset);
            Canvas.SetRight(FunctionMenuUpperRight, offset);
            Canvas.SetZIndex(FunctionMenuUpperRight, 10);

            FunctionMenuUpperRight.LayoutTransform = new RotateTransform(-90);

            FunctionMenuUpperLeft = null;
            FunctionMenuBottomRight = null;
            if (MenuType == CornerMenuType.FourCornerMenus)
            {
                // Upper Left Corner menu
                FunctionMenuUpperLeft = new CornerMenu(CornerMenu.CornerMenuLocation.TopLeft);
                InterfaceLayoutRoot.Children.Add(FunctionMenuUpperLeft);
                Canvas.SetTop(FunctionMenuUpperLeft, offset);
                Canvas.SetLeft(FunctionMenuUpperLeft, offset);
                Canvas.SetZIndex(FunctionMenuUpperLeft, 10);

                FunctionMenuUpperLeft.LayoutTransform = new RotateTransform(180);

                // Bottom Right Corner menu
                FunctionMenuBottomRight = new CornerMenu(CornerMenu.CornerMenuLocation.BottomRight);
                InterfaceLayoutRoot.Children.Add(FunctionMenuBottomRight);
                Canvas.SetBottom(FunctionMenuBottomRight, offset);
                Canvas.SetRight(FunctionMenuBottomRight, offset);
                Canvas.SetZIndex(FunctionMenuBottomRight, 10);
            }
        }

        /// <summary>
        /// Create the exit confirmation dialogs
        /// </summary>
        public void CreateExitDialogs()
        {
            if (MenuType == CornerMenuType.NoCornerMenus) return;

            var offset = 100.0;

            ExitDialogUpperRight = new ExitDialog("ExitDialogUpperRight");
            ExitDialogUpperRight.Visibility = Visibility.Hidden;
            Canvas.SetRight(ExitDialogUpperRight, offset);
            Canvas.SetTop(ExitDialogUpperRight, offset);
            Canvas.SetZIndex(ExitDialogUpperRight, 1);
            ExitDialogUpperRight.LayoutTransform = new RotateTransform(-90);
            InterfaceLayoutRoot.Children.Add(ExitDialogUpperRight);

            ExitDialogBottomLeft = new ExitDialog("ExitDialogBottomLeft");
            ExitDialogBottomLeft.Visibility = Visibility.Hidden;
            Canvas.SetLeft(ExitDialogBottomLeft, offset);
            Canvas.SetBottom(ExitDialogBottomLeft, offset);
            Canvas.SetZIndex(ExitDialogBottomLeft, 1);
            ExitDialogBottomLeft.LayoutTransform = new RotateTransform(90);
            InterfaceLayoutRoot.Children.Add(ExitDialogBottomLeft);

            ExitDialogUpperLeft = null;
            ExitDialogBottomRight = null;

            if (MenuType == CornerMenuType.FourCornerMenus)
            {
                ExitDialogUpperLeft = new ExitDialog("ExitDialogUpperLeft");
                ExitDialogUpperLeft.Visibility = Visibility.Hidden;
                Canvas.SetLeft(ExitDialogUpperLeft, offset);
                Canvas.SetTop(ExitDialogUpperLeft, offset);
                Canvas.SetZIndex(ExitDialogUpperLeft, 1);
                ExitDialogUpperLeft.LayoutTransform = new RotateTransform(180);
                InterfaceLayoutRoot.Children.Add(ExitDialogUpperLeft);

                ExitDialogBottomRight = new ExitDialog("ExitDialogBottomRight");
                ExitDialogBottomRight.Visibility = Visibility.Hidden;
                Canvas.SetRight(ExitDialogBottomRight, offset);
                Canvas.SetBottom(ExitDialogBottomRight, offset);
                Canvas.SetZIndex(ExitDialogBottomRight, 1);
                InterfaceLayoutRoot.Children.Add(ExitDialogBottomRight);
            }
        }

        #region Corner Button Functions

        /// <summary>
        /// returns whether any of the exit dialogs are visible
        /// </summary>
        /// <returns></returns>
        public bool isExitDialogVisible()
        {
            if (ExitDialogBottomLeft != null && ExitDialogBottomLeft.Visibility == Visibility.Visible ||
                ExitDialogBottomRight != null && ExitDialogBottomRight.Visibility == Visibility.Visible ||
                ExitDialogUpperLeft != null && ExitDialogUpperLeft.Visibility == Visibility.Visible ||
                ExitDialogUpperRight != null && ExitDialogUpperRight.Visibility == Visibility.Visible)
                return true;
            return false;
        }

        /// <summary>
        /// Make the Search Dialog Visible - Still under development
        /// </summary>
        public void viewSearchDialog()
        {
            //BackgroundMapLayer.AddSearchBox(new Point(500, 500));
        }


        /// <summary>
        /// This brings out a BookmarkListFrame with the same orientation
        /// of the touch finger.  The location of the list is close to the
        /// activating corner function menu.       
        /// </summary>
        /// <param name="orientation">Orientation of the List box</param>
        /// <param name="location">location of the originating corner menu</param>
        public void showListBox(double orientation, CornerMenu.CornerMenuLocation location)
        {
            switch (location)
            {
                case CornerMenu.CornerMenuLocation.TopLeft:
                    BackgroundMapLayer.Instance.AddBookmarkListFrame(new Point(200, 850), orientation);
                    break;
                case CornerMenu.CornerMenuLocation.TopRight:
                    BackgroundMapLayer.Instance.AddBookmarkListFrame(new Point(200, 200), orientation);
                    break;
                case CornerMenu.CornerMenuLocation.BottomLeft:
                    BackgroundMapLayer.Instance.AddBookmarkListFrame(new Point(550, 850), orientation);
                    break;
                case CornerMenu.CornerMenuLocation.BottomRight:
                    BackgroundMapLayer.Instance.AddBookmarkListFrame(new Point(550, 200), orientation);
                    break;
            }
        }

        /// <summary>
        /// Show the Map Layers Manager Panel
        /// </summary>
        /// <param name="location">Location of the originating corner menu</param>
        public void ShowMapLayersManagerPanel(CornerMenu.CornerMenuLocation location)
        {
            switch (location)
            {
                case CornerMenu.CornerMenuLocation.TopLeft:
                    BackgroundMapLayer.Instance.ShowMapLayersManagerPanel(new Point(350, 700), 90.0);
                    break;
                case CornerMenu.CornerMenuLocation.TopRight:
                    BackgroundMapLayer.Instance.ShowMapLayersManagerPanel(new Point(350, 700), 90.0);
                    break;
                case CornerMenu.CornerMenuLocation.BottomLeft:
                    BackgroundMapLayer.Instance.ShowMapLayersManagerPanel(new Point(350, 700), 270.0);
                    break;
                case CornerMenu.CornerMenuLocation.BottomRight:
                    BackgroundMapLayer.Instance.ShowMapLayersManagerPanel(new Point(350, 700), 270.0);
                    break;
            }
        }

        /// <summary>
        /// Show the Exit dialog
        /// </summary>
        /// <param name="orientation">Orientation of the exit dialog</param>
        /// <param name="location">location of the originating corner menu</param>
        public void showExitMenu(double orientation, CornerMenu.CornerMenuLocation location)
        {
            // FIX ME: fix the orientation of the exit confirmation dialog

            switch (location)
            {
                case CornerMenu.CornerMenuLocation.TopLeft:
                    //ExitDialogUpperLeft.RenderTransform = new RotateTransform(orientation);
                    ExitDialogUpperLeft.Visibility = Visibility.Visible;                    
                    break;
                case CornerMenu.CornerMenuLocation.TopRight:
                    //ExitDialogUpperRight.RenderTransform = new RotateTransform(orientation);
                    ExitDialogUpperRight.Visibility = Visibility.Visible;                    
                    break;
                case CornerMenu.CornerMenuLocation.BottomLeft:
                    //ExitDialogBottomLeft.RenderTransform = new RotateTransform(orientation);
                    ExitDialogBottomLeft.Visibility = Visibility.Visible;                    
                    break;
                case CornerMenu.CornerMenuLocation.BottomRight:
                    //ExitDialogBottomRight.RenderTransform = new RotateTransform(orientation);
                    ExitDialogBottomRight.Visibility = Visibility.Visible;                    
                    break;
            }
        }

        #endregion
    }
}
